package com.yunmang.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.google.gson.Gson;
import com.yunmang.DaoUtils.CommonDaoUtils;
import com.yunmang.R;
import com.yunmang.activity.ordermanager.OrderBudaDialog;
import com.yunmang.activity.ordermanager.UpdateOrderInfoActivity;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.NameBean;
import com.yunmang.entity.ParameterEntity.OrderLookBean;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.CommonUtils;
import com.yunmang.utils.ViewHolderUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

import de.greenrobot.event.EventBus;
import okhttp3.RequestBody;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/8.
 */
public class OrderManagerAdapter extends BaseAdapter implements OnItemClickListener{
    private Context context;
    private List<OrderLookBean.ModelBean.ItemsBean> lists;
    private AlertView delAlertView,printAlertView;
    private Subscriber<NameBean> subscriber;
    private String orderId;
    private int delPos;
    private SubBean subBean;
    public OrderManagerAdapter( Context context,List<OrderLookBean.ModelBean.ItemsBean> lists){
        this.context=context;
        this.lists=lists;
    }
    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        final OrderLookBean.ModelBean.ItemsBean itemsBean = lists.get(i);
        if(convertView==null)
            convertView=View.inflate(context, R.layout.item_order_manager,null);
        TextView order_hdbh= ViewHolderUtils.get(convertView,R.id.order_hdbh);
        TextView tv_start= ViewHolderUtils.get(convertView,R.id.tv_start);
        TextView tv_end= ViewHolderUtils.get(convertView,R.id.tv_end);
        TextView tv_date= ViewHolderUtils.get(convertView,R.id.tv_date);
        TextView tv_state= ViewHolderUtils.get(convertView,R.id.tv_state);
        TextView tv_goosname= ViewHolderUtils.get(convertView,R.id.tv_goosname);
        TextView tv_del= ViewHolderUtils.get(convertView,R.id.tv_del);
        TextView tv_edit= ViewHolderUtils.get(convertView,R.id.tv_edit);
        TextView tv_print= ViewHolderUtils.get(convertView,R.id.tv_print);
        final String hdbh=itemsBean.getHdbh();
        order_hdbh.setText(hdbh);

        tv_state.setText(itemsBean.getDisplayStatus());
        tv_goosname.setText(itemsBean.getGoodsName());
        tv_start.setText(itemsBean.getStartOrgName());
        tv_end.setText(itemsBean.getEndOrgName());
        tv_date.setText(itemsBean.getBillDate().substring(0,10));
        tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //弹出对话框提示是否要删除该条数据
                orderId=itemsBean.getId()+"";
                delPos=i;
                delAlertView=new AlertView("提示","确定作废"+hdbh+"这个单子？", "取消", new String[]{"确定"}, null, context, AlertView.Style.Alert,OrderManagerAdapter.this);
                delAlertView.setCancelable(true);
                delAlertView.show();
            }
        });
        tv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppConstant.itemsBean=itemsBean;
                context.startActivity(new Intent(context, UpdateOrderInfoActivity.class));
            }
        });
        tv_print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppConstant.itemsBean=itemsBean;
                alertShow1();
            }
        });
        return convertView;
    }

    @Override
    public void onItemClick(Object o, int position) {
        if(o==delAlertView&&position!=AlertView.CANCELPOSITION){
            actionZuoFei();
        }else if(o==printAlertView&&position!=AlertView.CANCELPOSITION){
            Intent in=new Intent(context,OrderBudaDialog.class);
            in.putExtra("type",position+"");
            context.startActivity(in);
        }
    }
    public void alertShow1() {
        printAlertView = new AlertView("标题", null, "取消", null, new String[]{"补打运单", "补打标签"}, context, AlertView.Style.ActionSheet, this);
        printAlertView.show();
    }


    private void actionZuoFei(){
        subscriber=new Subscriber<NameBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                CommonUtils.showToast(context,"网络出现错误");
            }

            @Override
            public void onNext(NameBean nameBean) {

                if(nameBean.getCode()==200){
                    CommonUtils.showToast(context,"作废成功");
                    lists.remove(delPos);
                    notifyDataSetChanged();
                }else if(nameBean.getCode()==300){
                    CommonUtils.showToast(context,"该票不能作废，请联系客服");
                }else{
                    CommonUtils.showToast(context,"作废失败");
                }
            }
        };
        subBean=new SubBean();
        subBean.id=orderId;
        subBean.userId= AppConstant.userid;
        Gson gson=new Gson();
        String inJson=gson.toJson(subBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), inJson);
        HttpUtils.getInstance().orderInvalid(subscriber, body);
    }
    public class SubBean{
        public String id;
        public String userId;
    }
}
